// 简单的图标生成脚本
const fs = require('fs');
const path = require('path');

// 创建一个简单的PNG图标（1x1像素的蓝色方块）
// 这是一个非常基础的实现，仅用于开发测试
const createSimplePNG = () => {
  // PNG文件头
  const signature = Buffer.from([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]);
  
  // IHDR chunk (图像头)
  const width = 512;
  const height = 512;
  const bitDepth = 8;
  const colorType = 2; // RGB
  const compression = 0;
  const filter = 0;
  const interlace = 0;
  
  const ihdrData = Buffer.alloc(13);
  ihdrData.writeUInt32BE(width, 0);
  ihdrData.writeUInt32BE(height, 4);
  ihdrData[8] = bitDepth;
  ihdrData[9] = colorType;
  ihdrData[10] = compression;
  ihdrData[11] = filter;
  ihdrData[12] = interlace;
  
  const ihdrCrc = 0x9DC6C723; // 预计算的CRC
  
  // 创建一个简单的蓝色PNG
  const pixelData = Buffer.alloc(width * height * 3 + height); // RGB + 每行一个过滤器字节
  for (let y = 0; y < height; y++) {
    pixelData[y * (width * 3 + 1)] = 0; // 过滤器类型 (无过滤)
    for (let x = 0; x < width; x++) {
      const idx = y * (width * 3 + 1) + 1 + x * 3;
      pixelData[idx] = 0x4A; // R
      pixelData[idx + 1] = 0x90; // G
      pixelData[idx + 2] = 0xE2; // B
    }
  }
  
  // 简化的IDAT chunk (不压缩，仅用于测试)
  const idatData = Buffer.concat([Buffer.from([0x78, 0x9C]), pixelData, Buffer.from([0x01, 0x00, 0x00])]);
  const idatCrc = 0x0; // 简化，实际应该计算CRC
  
  // IEND chunk
  const iendData = Buffer.alloc(0);
  const iendCrc = 0xAE426082;
  
  // 组合PNG文件
  const png = Buffer.concat([
    signature,
    Buffer.from([0x00, 0x00, 0x00, 0x0D]), // IHDR length
    Buffer.from('IHDR'),
    ihdrData,
    Buffer.from([0x9D, 0xC6, 0xC7, 0x23]), // IHDR CRC
    Buffer.from([0x00, 0x00, 0x00, 0x00]), // IDAT length (简化)
    Buffer.from('IDAT'),
    idatData,
    Buffer.from([0x00, 0x00, 0x00, 0x00]), // IDAT CRC (简化)
    Buffer.from([0x00, 0x00, 0x00, 0x00]), // IEND length
    Buffer.from('IEND'),
    Buffer.from([0xAE, 0x42, 0x60, 0x82])  // IEND CRC
  ]);
  
  return png;
};

// 创建图标文件
const iconPath = path.join(__dirname, '../public/icons/icon.png');
const iconData = createSimplePNG();
fs.writeFileSync(iconPath, iconData);

console.log('Created simple icon at:', iconPath);